% Adding noise
% Now simple random noise is added
% randomly to ts subsections

%latest date 6.3.3


function[TS,ProcDef] = PutNoise(TS,ProcDef);

A = std(TS.matrix);% standard deviation - an idea about varaince
block = ProcDef.block;
Frq = ProcDef.TLFreq1([3 9]); %
fs = TS.head.fs;
t = (1:ProcDef.block)/fs;
nsamples = length(TS.matrix);
len = nsamples/ProcDef.block;
j=1;
x = (rand([ProcDef.block 1])-0.5);
%   + sin(2*pi*t*Frq(1))'+ sin(2*pi*t*Frq(2))';
%NM = [A(1)*x A(2)*x A(3)*x A(4)*x A(5)*x];
for i = 1:ProcDef.block:nsamples-ProcDef.block,
   s = rand;
   if s>0.8,
 	   for k = 1:length(A),
      	MU = mean(TS.matrix(i:i+(ProcDef.block-1),k));
  			SIGMA = std(TS.matrix(i:i+(ProcDef.block-1),k));
			NM = (normrnd(MU,SIGMA,[ProcDef.block 1]));
	      %  + sin(2*pi*t*Frq(1))'+ sin(2*pi*t*Frq(2))';
      	TS.matrix(i:i+(ProcDef.block-1),k) = TS.matrix(i:i+(ProcDef.block-1),k)+ floor(NM);
         ProcDef.SelStacks(j) = 0;
         end;
	end;

   s=rand;
   if s<0.2,
      for k = 1:length(A),
	     	b = floor(rand*7+3);
   	   a = floor(rand*(block-b));
      	SIGMA = std(TS.matrix(i+a:i+a+b,k));
         TS.matrix(i+a:i+a+b,k)=spike(SIGMA,TS.matrix(i+a:i+a+b,k));
          ProcDef.SelStacks(j) = 0;
         fprintf('%d ',i);
      end;
      
   end;
j=j+1;
end;
   
                                                                                                                                                                                                                                                                                                                                                                                                                                     